﻿
using FB.Enums;
using FB.Mvc.Controllers;
using FB.Providers;
using FB.Providers.EmployeeInfo;
using FCDB.Entity;
using FCDB.Entity.EntityEx;
using FCDB.Repository;
using FCDB.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace FCDB.Web.Controllers
{
     [FB.Membership.Annotations.AuthorizeOnly]
    public class DepartmentController : BaseController
    {
        public ActionResult Index()
        {
            var efemployeeInfoProvider = AspNetEmployeeInfoProviderManager.Default as EFEmployeeInfoProvider;
            EmployeeInfoUnit unitinfo = efemployeeInfoProvider.GetEmployeeInfoUnit(); 
            IList<Department> deptList = null;
            IList<DepartmentModel> deptModelList = new List<DepartmentModel>();
            IList<Company> companyList = null;
            if (string.IsNullOrEmpty(Request["CompanyID"]))
            {
                companyList = unitinfo.CompanyRepository.Get(t => t.RecordState == RecordState.Active , m => m.OrderBy(t => t.Order)).ToList();
                Company RootCompany = companyList.FirstOrDefault(t => t.ParentCOMCode == "" || t.ParentCOMCode == null);
                ViewData["CompanyList"] = companyList;
                ViewBag.RootCompanyID = RootCompany.COMCode;
                deptList = unitinfo.DepartmentRepository.Get(t => t.COMCode == RootCompany.COMCode&& t.RecordState == RecordState.Active, m => m.OrderBy(t => t.Order)).ToList();
            }
            else
            {
                string companyID = Request["CompanyID"];
                deptList = unitinfo.DepartmentRepository.Get(t => t.COMCode == companyID && t.RecordState == RecordState.Active, m => m.OrderBy(t => t.Order)).ToList();
            }
            foreach (Department item in deptList)
            {
                DepartmentModel model = new DepartmentModel(item);
                deptModelList.Add(model);
            }
            if (Request.IsAjaxRequest())
            {
                return PartialView("UCDepartmentTree", deptModelList);
            }
            DepartmentModel dept = deptModelList.FirstOrDefault(t => t.ParentDEPTCode == null || t.ParentDEPTCode == "");
            if (dept == null) dept = new DepartmentModel();
            ViewData["RootNode"] = dept;
            ViewBag.RootNodeID = dept.DEPTCode;
            return View(deptModelList);
        }
        public ActionResult Detail(string id)
        {
            var efemployeeInfoProvider = AspNetEmployeeInfoProviderManager.Default as EFEmployeeInfoProvider;
            EmployeeInfoUnit unitinfo = efemployeeInfoProvider.GetEmployeeInfoUnit(); 
            if (!string.IsNullOrEmpty(id))
            {
                Department dept = unitinfo.DepartmentRepository.GetByKey(id);
                DepartmentModel model = null;
                if (dept == null)
                {
                    model = new DepartmentModel();
                }
                else
                {
                    model = new DepartmentModel(dept);
                }
                return PartialView("UCIndex", model);
            }
            return null;
        }

        public ActionResult Create()
        {
            var efemployeeInfoProvider = AspNetEmployeeInfoProviderManager.Default as EFEmployeeInfoProvider;
            EmployeeInfoUnit unitinfo = efemployeeInfoProvider.GetEmployeeInfoUnit(); 
            if (string.IsNullOrEmpty(Request["ID"]))
            {
                if (string.IsNullOrEmpty(Request["ParentID"]))
                {
                    ViewBag.ParentDepartment = new Department() { Name = "无上级" };// DepartmentManager.GetDepartment(parentID); 
                }
                else
                {
                    string parentID = Request["ParentID"];
                    ViewBag.ParentDepartment = unitinfo.DepartmentRepository.GetByKey(parentID);
                }
                Department dept = new Department();
                dept.COMCode = Request["CompanyID"];
                return View(dept);
            }
            else
            {
                string id = Request["ID"];
                Department department = unitinfo.DepartmentRepository.GetByKey(id);
                if (string.IsNullOrEmpty(department.ParentDEPTCode))
                {
                    ViewBag.ParentDepartment = new Department() { Name = "无上级" };
                }
                else
                {
                    ViewBag.ParentDepartment = unitinfo.DepartmentRepository.GetByKey(department.DEPTCode);
                }

                return View(department);
            }
        }

        public ActionResult Save(Department model)
        {
            var efemployeeInfoProvider = AspNetEmployeeInfoProviderManager.Default as EFEmployeeInfoProvider;
            EmployeeInfoUnit unitinfo = efemployeeInfoProvider.GetEmployeeInfoUnit(); 
            RtnMsg msg = new RtnMsg() { Status = 1 };
            if (model == null)
            {
                msg.Status = 3;
                return Json(msg, JsonRequestBehavior.AllowGet);
            }
            if (string.IsNullOrEmpty(model.CreatedBy))
            { 
                unitinfo.DepartmentRepository.Insert(model);
                unitinfo.SaveChanges();
                return Json(msg, JsonRequestBehavior.AllowGet);
            }
            Department updatemodel = unitinfo.DepartmentRepository.GetByKey(model.DEPTCode);
            updatemodel.Name = model.Name;
            updatemodel.Order = model.Order;
            unitinfo.DepartmentRepository.Update(updatemodel);
            unitinfo.SaveChanges();
            return Json(msg, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Delete(string id)
        {
            var efemployeeInfoProvider = AspNetEmployeeInfoProviderManager.Default as EFEmployeeInfoProvider;
            EmployeeInfoUnit unitinfo = efemployeeInfoProvider.GetEmployeeInfoUnit(); 
            Department updatemodel = unitinfo.DepartmentRepository.GetByKey(id);
            updatemodel.RecordState = RecordState.Deleted;
            unitinfo.DepartmentRepository.Update(updatemodel);
            unitinfo.SaveChanges();
            return null;
        }

        public ActionResult CheckDepartmentCode(string DEPTCode, string CreatedBy)
        {
            var efemployeeInfoProvider = AspNetEmployeeInfoProviderManager.Default as EFEmployeeInfoProvider;
            EmployeeInfoUnit unitinfo = efemployeeInfoProvider.GetEmployeeInfoUnit(); 
            Department entity = null;
            List<object> list = new List<object>();
            list.Add("DEPTCode");
            entity = unitinfo.DepartmentRepository.GetByKey(DEPTCode);
            bool canAdd = entity != null && string.IsNullOrEmpty(CreatedBy);
            list.Add(!canAdd);
            if (canAdd == true)
            {
                list.Add("ajaxCheckDepartmentCode");
            }

            return Json(list, JsonRequestBehavior.AllowGet);
        }




         
    }
}
